(function(){
        //日期选择函数
        function GetAnalysisWindowFiterTimes(valuetoid, type, event) {
            var ntime = new Date();
            if (type == 'd') {
                  var dstart = ntime.format("MM/dd/yyyy");
                  var dend = ntime.format("MM/dd/yyyy");
                  $(".timeControl #" + valuetoid).val(dstart + " - " + dend);
            } else if (type == 'w') {
                  var wstart = new Date(ntime.getTime() + (24 * 60 * 60 * -6 * 1000)).format("MM/dd/yyyy");
                  var wend = ntime.format("MM/dd/yyyy");
                  $(".timeControl #" + valuetoid).val(wstart + ' - ' + wend);
            } else if (type == 'm') {
                  var mstart = new Date(ntime.getTime() + (24 * 60 * 60 * -30 * 1000)).format("MM/dd/yyyy");
                  var mend = ntime.format("MM/dd/yyyy");
                  $(".timeControl #" + valuetoid).val(mstart + ' - ' + mend);
            }
            eval(event);
      }
      GetAnalysisWindowFiterTimes('table_time', 'm', false); 
        //选择日期触发事件
      $('.timeControl #table_time').daterangepicker(null, function (start, end, label) {
            times = $(".timeControl #table_time").val();
            getTableData(times);
      });  
      function getTableData(val){
            let times;
            if(val ==undefined){
                  times=$(".timeControl #table_time").val();
            }else{
                  times=val;
            }
            var startTimeSplit = times.split('-')[0].replace(" ", "").split('/'),
            endTimeSplit = times.split('-')[1].replace(" ", "").split('/');
            var startTime = startTimeSplit[2] + "-" + startTimeSplit[0] + "-" + startTimeSplit[1];
            var endTime = endTimeSplit[2] + "-" + endTimeSplit[0] + "-" + endTimeSplit[1];
            layui.use(['table','layer','form','layedit','laydate'], function(){
                  var table = layui.table;
                  var layer=layui.layer;
                  var layedit = layui.layedit;
                  var form=layui.form;
                  var $=layui.$;
                  var laydate = layui.laydate;
                  table.render({
                        elem: '#idTest',
                        url:'http://192.168.9.59:18902/patrol/findByValues',
                        contentType: "application/x-www-form-urlencoded",
                        page: { 
                              layout: [ 'prev', 'page', 'next', 'skip','limit', 'count',] //自定义分页布局
                              , curr: 1 //设定初始在第 1 页
                              , limit: 10//每页多少数据
                              , groups: 5 //只显示 5 个连续页码
                              ,limits:[5,10,15]//每页条数的选择项 
                        },
                        where: {startTime:startTime,endTime:endTime},
                        parseData: function(res){ //res 即为原始返回的数据
                                    return {
                                          "code": res.code, //解析接口状态
                                          "msg": res.msg, //解析提示文本
                                          "count": res.total, //解析数据长度
                                          "data": res.data //解析数据列表
                                    };
                              },
                        cols: [[
                                    ,{field:'name',width:200, title: '站点名称'}
                                    ,{field:'log',width:60, title: '经度'}
                                    ,{field:'lat',width:60, title: '纬度'}
                                    ,{field:'remark', title: '巡查内容'}
                                    ,{field:'man', title: '巡查人'}
                                    ,{field:'time1', title: '检测日期'} 
                                    ,{field:'time2',title: '填报日期'}
                                    ,{fixed: 'right', title:'操作', toolbar: '#barDemo',align:'center'}
                                    //站点名称，经度，纬度，巡查内容，巡查人，检测日期，填报日期
                              ]]
                  });  
                  table.on('tool(test)', function(obj){
                        var data = obj.data;
                        if(obj.event === 'delete'){
                              layer.confirm("你确定要删除么？",{btn:['确认','取消']},
                              function(){
                                    $.post("http://192.168.9.59:18902/patrol/delPatrl",{id:data.id}, function (res) {
                                          if(res.state==0){
                                                // 删除成功刷新table
                                                getTableData();  
                                          }
                                          layer.msg(res.msg)
                                    
                                     });
                              }
                          )
                        }else if(obj.event === 'edit'){
                              let arr={};
                              $.post("http://192.168.9.59:18902/patrol/findById",{id:data.id}, function (res) {
                                    if(res.msg=="查询成功"){
                                          editTable(res.data[0]);  
                                    }
                                    // layer.msg(res.msg)
                               });

                        }else if(obj.event === 'see'){

                              $.post("http://192.168.9.59:18902/patrol/findById",{id:data.id}, function (res) {
                                    if(res.msg=="查询成功"){
                                          seeTable(res.data[0]);  
                                    }
                                    // layer.msg(res.msg)
                               });
                           

                        }
                    })  
            })
      }
      getTableData();
  
      $("#add").click(()=>{
            let fromArr=['stationName','lng','lat','patrolMsg','patrolerName','monitorTime','submitTime','NI','NO','EI','EO','HI','HO','DN','DE','DH','DF','DG','id'];
            fromArr.forEach((li)=>{
                  $('#'+li).val("");
                  $('#'+li).removeAttr("disabled");
            })
            layui.use(['table','layer','form','layedit','laydate'], function(){
                  var table = layui.table;
                  var layer=layui.layer;
                  var layedit = layui.layedit;
                  var form=layui.form;
                  var $=layui.$;
                  var laydate = layui.laydate;
                  layer.open({
                        type: 1,
                        title: ['新增','background-color: #3e4854;color:#fff'],
                        shadeClose: true,
                        shade: false,
                        skin:'layer-extskin-overauto',
                        maxmin: true, //开启最大化最小化按钮
                        area: ['925px', 'auto'],
                        content: $("#addFrom"),
                        btn: ['保存','关闭'],
                        success:function(layero, index){
                              layui.use(['laydate'], function(){
                                    var laydate = layui.laydate;
                                    laydate.render({
                                          elem: '#monitorTime',
                                          type: 'datetime',
                                          format: 'yyyy-MM-dd HH:mm:ss' 
                                    });
                                    laydate.render({
                                          elem: '#submitTime',
                                          type: 'datetime',
                                          format: 'yyyy-MM-dd HH:mm:ss' 
                                    });
                              });
                              // $("#fromVal").addClass("layui-from");
                              layero.find(".layui-layer-btn0").attr("lay-filter","fromVerify").attr("lay-submit","")
                        },
                        yes: function(index, layero){
                              // 验证成功后执行操作
                              form.on('submit(fromVerify)',function () {
                                    // var temp=$('#fromVal').val();
                                    var temp=getElements("fromVal")
                                    addOrUpdate(temp);
                                    return false;
                              })
                        },
                        btn2: function(){
                              layer.closeAll();
                        }
                  }); 

      })

})

      function editTable(data){
            let fromArr=['stationName','lng','lat','patrolMsg','patrolerName','monitorTime','submitTime','NI','NO','EI','EO','HI','HO','DN','DE','DH','DF','DG','id'];
            fromArr.forEach((li)=>{
                  $('#'+li).removeAttr("disabled");
            })
            if(data != {}){
                  layui.use(['table','layer','form','layedit','laydate'], function(){
                        var table = layui.table;
                        var layer=layui.layer;
                        var layedit = layui.layedit;
                        var form=layui.form;
                        var $=layui.$;
                        var laydate = layui.laydate;
            
                        layer.open({
                              type: 1,
                              title: ['编辑','background-color: #3e4854;color:#fff'],
                              shadeClose: true,
                              shade: false,
                              skin:'layer-extskin-overauto',
                              maxmin: true, //开启最大化最小化按钮
                              area: ['925px', 'auto'],
                              content: $("#addFrom"),
                              btn: ['保存','关闭'],
                              success:function(layero, index){
                                    layui.use(['laydate'], function(){
                                          var laydate = layui.laydate;
                                          laydate.render({
                                                elem: '#monitorTime',
                                                type: 'datetime',
                                                format: 'yyyy-MM-dd HH:mm:ss' 
                                          });
                                          laydate.render({
                                                elem: '#submitTime',
                                                type: 'datetime',
                                                format: 'yyyy-MM-dd HH:mm:ss' 
                                          });
                                    });
                                    for(let i in data){
                                          $('#'+ i).val(data[i]);
                                    }
                                   
                                    layero.find(".layui-layer-btn0").attr("lay-filter","fromVerify").attr("lay-submit","")
                              },
                              yes: function(index, layero){
                                    // 验证成功后执行操作
                                    form.on('submit(fromVerify)',function () {
                                          // var temp=$('#fromVal').val();
                                          var temp=getElements("fromVal")
                                          saveOrUpdate(temp);
                                          return false;
                                    })
                              },
                              btn2: function(){
                                    layer.closeAll();
                              }
                        }); 
                  
                  })

                

            }
      }
      function seeTable(data){
            let fromArr=['stationName','lng','lat','patrolMsg','patrolerName','monitorTime','submitTime','NI','NO','EI','EO','HI','HO','DN','DE','DH','DF','DG','id'];
            fromArr.forEach((li)=>{
                  $('#'+li).attr("disabled","disabled");
            })
            if(data != {}){
                  layui.use(['table','layer','form','layedit','laydate'], function(){
                        var table = layui.table;
                        var layer=layui.layer;
                        var layedit = layui.layedit;
                        var form=layui.form;
                        var $=layui.$;
                        var laydate = layui.laydate;
            
                        layer.open({
                              type: 1,
                              title: ['查看','background-color: #3e4854;color:#fff'],
                              shadeClose: true,
                              shade: false,
                              skin:'layer-extskin-overauto',
                              maxmin: true, //开启最大化最小化按钮
                              area: ['925px', 'auto'],
                              content: $("#addFrom"),
                              success:function(layero, index){
                                    layui.use(['laydate'], function(){
                                          var laydate = layui.laydate;
                                          laydate.render({
                                                elem: '#monitorTime',
                                                type: 'datetime',
                                                format: 'yyyy-MM-dd HH:mm:ss' 
                                          });
                                          laydate.render({
                                                elem: '#submitTime',
                                                type: 'datetime',
                                                format: 'yyyy-MM-dd HH:mm:ss' 
                                          });
                                    });
                                    for(let i in data){
                                          $('#'+ i).val(data[i]);
                                    }

                              },
                            
                        }); 
                  
                  })

                

            }
      }
      function getElements(formId) {
            var form = document.getElementById(formId);
            if(form == null){
                  return false;
            }
            var elements = {};
            var inputTagElements = form.getElementsByTagName('input');
            for (var j = 0; j < inputTagElements.length; j++) {
                  elements[inputTagElements[j].name] =inputTagElements[j].value;
            }
            return elements;
      }
      function addOrUpdate(data){
            //192.168.0.114:18902
            //192.168.9.59:18902
            delete data.id;
            $.post("http://192.168.9.59:18902/patrol/addPatrol",data, function (res) {
                        if(res.state ==0){
                              getTableData();
                              layer.closeAll(); 
                        }
                        layer.msg(res.msg)
            });
      }
      function saveOrUpdate(data){
            //192.168.0.114:18902
            //192.168.9.59:18902
            $.post("http://192.168.9.59:18902/patrol/savePatrol",data, function (res) {
                        if(res.state ==0){
                              getTableData();
                              layer.closeAll(); 
                        }
                        layer.msg(res.msg)
            });
      }
})();

